{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fcfa2354",
   "metadata": {},
   "source": [
    "## 5.数组形状重塑、元素修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7e6980d5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.81533415],\n",
       "       [0.01302308],\n",
       "       [0.86337303],\n",
       "       [0.92991119],\n",
       "       [0.1167054 ],\n",
       "       [0.274833  ],\n",
       "       [0.91938546],\n",
       "       [0.81248249],\n",
       "       [0.94697091],\n",
       "       [0.62099591],\n",
       "       [0.66241327],\n",
       "       [0.66474786]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#reshape\n",
    "import numpy as np\n",
    "A=np.random.rand(3,4)\n",
    "B=A.reshape((12,1))\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "aa9db04b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        ],\n",
       "       [1.        ],\n",
       "       [1.        ],\n",
       "       [1.        ],\n",
       "       [1.        ],\n",
       "       [0.274833  ],\n",
       "       [0.91938546],\n",
       "       [0.81248249],\n",
       "       [0.94697091],\n",
       "       [0.62099591],\n",
       "       [0.66241327],\n",
       "       [0.66474786]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B[:5,0]=1\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b821cb81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 1.        , 1.        , 1.        ],\n",
       "       [1.        , 0.274833  , 0.91938546, 0.81248249],\n",
       "       [0.94697091, 0.62099591, 0.66241327, 0.66474786]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[0,:]=1\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "47213346",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.        , 1.        , 1.        , 1.        , 1.        ,\n",
       "       0.274833  , 0.91938546, 0.81248249, 0.94697091, 0.62099591,\n",
       "       0.66241327, 0.66474786])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#flatten 一维扁平化\n",
    "A.flatten()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63d4e546",
   "metadata": {},
   "source": [
    "## 6.叠加重复数组\n",
    "\n",
    "### 6.1 tile and repeat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "00b4014b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2], [3, 4]])\n",
    "np.repeat(a, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3ddb31cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 1, 2, 1, 2],\n",
       "       [3, 4, 3, 4, 3, 4]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tile(a, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22105e27",
   "metadata": {},
   "source": [
    "tile也可以改变规模"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "65c4a447",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 1, 2, 1, 2],\n",
       "       [3, 4, 3, 4, 3, 4]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tile(a, (1, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "06ac9e71",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [1, 2],\n",
       "       [3, 4],\n",
       "       [1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tile(a, (3, 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "922974d3",
   "metadata": {},
   "source": [
    "### 6.2 concatenate 拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2975a2d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#在第0维拼接\n",
    "b = np.array([[5, 6]])\n",
    "np.concatenate((a,b),axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4adbce0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 5],\n",
       "       [3, 4, 6]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((a,b.T),axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26bd73d7",
   "metadata": {},
   "source": [
    "## 7.复制和深度复制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eae60191",
   "metadata": {},
   "source": [
    "一般相等的复制是把两个数组完全等价，但是修改一个的时候另一个也会发生变化，所以要把两个数组分隔开来，就需要使用深度复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4323034d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10  2]\n",
      " [ 3  4]]\n",
      "[[1 2]\n",
      " [3 4]]\n"
     ]
    }
   ],
   "source": [
    "c=np.copy(a)\n",
    "c[0,0]=10\n",
    "print(c)\n",
    "print(a)#修改c不会影响a的取值"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
